﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace BYFW.Model.Authorization
{
    /// <summary>
    /// 角色－用户关系类
    /// </summary>
    public class RURelation : BOL.BaseEntity
    {
        static RURelation()
        {
            try
            {
                s_enableTableCache = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["RUEnableTableCache"]);
            }
            catch
            {
            }
        }
        public RURelation()
        {
            m_tableName = "auth_ru";
            m_tblCacheManager.IsEnable = s_enableTableCache;
        }

        private static bool s_enableTableCache = true;

        protected int m_uid = 0;
        protected int m_rid = 0;

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserID
        {
            get { return m_uid; }
            set { m_uid = value; }
        }
        /// <summary>
        /// 角色ID
        /// </summary>
        public int RoleID
        {
            get { return m_rid; }
            set { m_rid = value; }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("uid", m_uid);
            m_container.Add("rid", m_rid);
        }
        public override bool GetProperties(DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_uid = this.GetIntValue(record, "uid");
            m_rid = this.GetIntValue(record, "rid");
            return true;
        }

        /// <summary>
        /// 获取符合各种条件的记录
        /// </summary>
        /// <param name="userID">用户ID，当为null时，忽略此条件</param>
        /// <param name="roleID">角色ID，当为null时，忽略此条件</param>
        /// <returns></returns>
        public DataTable GetRecords(int? userID, int? roleID)
        {
            string cond = "";
            if (userID != null)
            {
                cond += "uid=" + userID;
            }
            if (roleID != null)
            {
                if (cond.Length > 0)
                {
                    cond += " AND ";
                }
                cond += "rid=" + roleID;
            }
            if (cond != "")
            {
                cond = " WHERE " + cond;
            }
            return this.GetRecords(cond);
        }

        /// <summary>
        /// 获取多个角色对应的多个用户记录
        /// </summary>
        /// <param name="roleIDs">角色ID列表</param>
        /// <returns></returns>
        public DataTable GetRecords(List<int> roleIDs)
        {
            string cond = " WHERE rid IN ({0})";
            string rids = "";
            for (int i = 0; i < roleIDs.Count; i++)
            {
                rids += DB.GetVal(roleIDs[i]) + ",";
            }
            rids = rids.TrimEnd(',').Trim();
            if (rids == "")
            {
                rids = DB.GetVal(0);
            }
            return this.GetRecords(string.Format(cond, rids));
        }

        /// <summary>
        /// 获取指定角色的角色关系类
        /// </summary>
        /// <param name="roleID">用户ID</param>
        /// <returns></returns>
        public List<RURelation> GetListByRID(int roleID)
        {
            List<RURelation> res = new List<RURelation>();
            DataTable dt = this.GetRecords(null, roleID); 
            for (int i = 0; i < dt.DefaultView.Count; i++)
            {
                RURelation ett = new RURelation();
                ett.GetProperties(dt.DefaultView[i].Row);
                res.Add(ett);
            }
            return res;
        }
        /// <summary>
        /// 获取指定用户的角色关系类
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public List<RURelation> GetListByUID(int userID)
        {
            List<RURelation> res = new List<RURelation>();
            DataTable dt = this.GetRecords(userID, null); 
            for (int i = 0; i < dt.DefaultView.Count; i++)
            {
                RURelation ett = new RURelation();
                ett.GetProperties(dt.DefaultView[i].Row);
                res.Add(ett);
            }
            return res;
        }
    }
}
